#include "ChangePassDialog.h"
#include "ui_ChangePassDialog.h"
#include <QFuture>

ChangePassDialog::ChangePassDialog(DataProvider* provider, QWidget *parent) :
	QDialog(parent),
	ui(new Ui::ChangePassDialog),
	_provider(provider)
{
	ui->setupUi(this);

	ui->comboPrevType->insertItem(0,tr("None"),CipherType::None);
	ui->comboPrevType->insertItem(1,"RC4",CipherType::RC4);
	ui->comboPrevType->insertItem(2,"DES",CipherType::DES);
	ui->comboPrevType->insertItem(3,"AES",CipherType::AES);

	ui->comboNewType->insertItem(0,tr("None"),CipherType::None);
	ui->comboNewType->insertItem(1,"RC4",CipherType::RC4);
	ui->comboNewType->insertItem(2,"DES",CipherType::DES);
	ui->comboNewType->insertItem(3,"AES",CipherType::AES);

	ui->resultLabel->setVisible(false);

	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	_movie = new QMovie(":/gif/rsc/waiting_23.gif");
	ui->gifLabel->setMovie(_movie);
	ui->gifLabel->setVisible(false);

	_thread = new PassProc(_provider, this);

	connect(ui->btnCancel,SIGNAL(clicked()),SLOT(onBtnCancel()));
	connect(ui->btnChangePass,SIGNAL(clicked()),SLOT(onBtnChange()));
	connect(_thread,SIGNAL(done()),SLOT(done()));
	connect(_thread,SIGNAL(showMessage(QString)),_provider,SLOT(showMessage(QString)));
}

ChangePassDialog::~ChangePassDialog()
{
	delete ui;
}

void ChangePassDialog::startGif()
{
	ui->gifLabel->setVisible(true);
	_movie->start();
}

void ChangePassDialog::stopGif()
{
	_movie->stop();
	ui->gifLabel->setVisible(false);
}

void ChangePassDialog::done()
{
	ui->resultLabel->setVisible(true);
	stopGif();
	_provider->loadDatabase();
	CipherType::Enum type = (CipherType::Enum) ui->comboNewType->itemData(ui->comboNewType->currentIndex()).value<int>();
	setCurrentPass(type, ui->editNewPass->text());
	ui->editNewPass->setText("");

	emit passChaged(type, ui->editPrevPass->text());
}

void ChangePassDialog::onBtnChange()
{
	ui->resultLabel->setVisible(false);
	startGif();
	CipherType::Enum type1 = (CipherType::Enum) ui->comboPrevType->itemData(ui->comboPrevType->currentIndex()).value<int>();
	CipherType::Enum type2 = (CipherType::Enum) ui->comboNewType->itemData(ui->comboNewType->currentIndex()).value<int>();
	_thread->setData(type1, type2, ui->editPrevPass->text(), ui->editNewPass->text());
	_thread->start();
}

void ChangePassDialog::onBtnCancel()
{
	stopGif();
	_thread->stop();
	close();
}

void ChangePassDialog::setCurrentPass(CipherType::Enum type, QString pass)
{
	int index = ui->comboPrevType->findData(type);
	if(index != -1){
		ui->comboPrevType->setCurrentIndex(index);
		ui->comboNewType->setCurrentIndex(index);
	}

	ui->editPrevPass->setText(pass);
}

//*****************************************************************

PassProc::PassProc(DataProvider* provider, QObject *parent) :
	QThread(parent),
	_provider(provider),
	_isRunning(true),
	_prevType(CipherType::None),
	_newType(CipherType::None)
{
}

void PassProc::setData(CipherType::Enum prevType, CipherType::Enum newType, QString prevPass, QString newPass)
{
	_prevType = prevType; _prevPass = prevPass;
	_newType = newType; _newPass = newPass;
}

void PassProc::stop()
{
	_isRunning = false;
}

void PassProc::run()
{
	// make backup
	QString filename = _provider->getDatabase()->getCurrentFile();

	QFile file(filename);
	if(!file.exists()){
		emit showMessage(QString(tr("Can't open database (%1)")).arg(filename));
		return;
	}

	QDir dir;
	dir.mkdir("./backup");
	QString backupFile = QString("./backup/backup_%1_%2.mdb")
			.arg(QDate::currentDate().toString("dd-MM-yyyy"))
			.arg(QTime::currentTime().toString("hh-mm-ss"));
	if( !file.copy(backupFile) ){
		emit showMessage(QString(tr("Can't copy database (%1)")).arg(filename));
		return;
	}
	file.close();

	// copy data
	QList<Note*> notes = _provider->getDatabase()->getNotesList();

	_provider->getDatabase()->clearAll();
	_provider->getCrypt()->setPassword(_newType, _newPass);
	foreach(Note* note, notes){
		_provider->getDatabase()->addNote(*note);
	}

	msleep(500);
	emit done();

}

